import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bugly/flutter_bugly.dart';
import 'package:palmf_hid/common/langs/translation_service.dart';
import 'package:palmf_hid/common/routes/routes.dart';
import 'package:palmf_hid/common/store/store.dart';
import 'package:palmf_hid/common/utils/logger.dart';
import 'package:palmf_hid/common/values/my_color.dart';
import 'package:palmf_hid/global.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:lifecycle/lifecycle.dart';
import 'package:palmf_hid/pages/main/on_init_main.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';


Future<void> main() async {
  FlutterBugly.postCatchedException(() async {
    await Global.init();
    runApp(MyApp());
    if(GetPlatform.isAndroid){
      initBugly();
    }
  });
}

class MyApp extends StatelessWidget {
  //警告:不要随意调整调用BotToastInit函数的位置
  final easyLoading = EasyLoading.init();

  MyApp({Key? key, }) : super(key: key); //1.调用BotToastInit

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      minTextAdapt: true,
      splitScreenMode: true,
      designSize: const Size(375, 800),
      builder: (ctx, wid) {
        return RefreshConfiguration(
            headerBuilder: () => const ClassicHeader(
                  completeText: "刷新完成",
                  releaseText: "下拉刷新",
                  refreshingText: "刷新中...",
                  idleText: "下来刷新",
                  failedText: "刷新失败",
                ),
            footerBuilder: () => CustomFooter(
                  builder: (BuildContext? context, LoadStatus? mode) {
                    Widget body;
                    if (mode == LoadStatus.idle) {
                      body = const Text("上拉加载",
                          style: TextStyle(
                              fontSize: 14, color: MyColor.c_66000000));
                    } else if (mode == LoadStatus.loading) {
                      body = const CupertinoActivityIndicator();
                    } else if (mode == LoadStatus.failed) {
                      body = const Text("加载失败！点击重试！",
                          style: TextStyle(
                              fontSize: 14, color: MyColor.c_66000000));
                    } else if (mode == LoadStatus.canLoading) {
                      body = const Text("松手,加载更多!",
                          style: TextStyle(
                              fontSize: 14, color: MyColor.c_66000000));
                    } else {
                      body = const Text(
                        "没有更多数据了!",
                        style:
                            TextStyle(fontSize: 14, color: MyColor.c_66000000),
                      );
                    }
                    return SizedBox(
                      height: 55.0,
                      child: Center(child: body),
                    );
                  },
                ),
            hideFooterWhenNotFull: false,
            headerTriggerDistance: 20,
            maxOverScrollExtent: 30,
            footerTriggerDistance: 20,
            child: GetMaterialApp(
              title: "GT80",
              theme:
                  ThemeData(useMaterial3: false, brightness: Brightness.light),
              darkTheme: ThemeData.light(),
              debugShowCheckedModeBanner: false,
              initialRoute: AppPages.INITIAL,
              getPages: AppPages.routes,
              builder: (context, child) {
                child = easyLoading(context, child);
                return MediaQuery(
                  //Setting font does not change with system font size
                  data: MediaQuery.of(context).copyWith(textScaler: TextScaler.linear(1.0)),
                  child: child,
                );
              },
              translations: TranslationService(),
              navigatorObservers: [
                AppPages.observer,
                defaultLifecycleObserver
              ],
              //国际化工厂代理
              localizationsDelegates: const [
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              supportedLocales: ConfigStore.to.languages,
              locale: ConfigStore.to.locale,
              defaultTransition: Transition.fade,
              fallbackLocale: const Locale('zh', 'CN'),
              enableLog: true,
              logWriterCallback: Logger.write,
            ));
      },
    );
  }
}
